package com.ssy.lingxi.marketing.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ssy.lingxi.marketing.api.model.constant.MkFeignConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.Map;

/**
 *  营销服务配置
 * @author ds
 * @date 2022/3/8
 * @version 2.0.0
 */
@Configuration
public class MarketingServiceConfig {

    /**
     * QueryDsl Bean 配置
     * @param entityManager Jpa的EntityManager
     * @return QueryDsl JpaQueryFactory，直接注入使用
     */
    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }


    /*********************************** 营销活动-延时队列（拼团过期/） *****************************************/
    @Bean
    public CustomExchange mk_delay_exchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MkFeignConstants.MK_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue mk_delay_queue() {
        return new Queue(MkFeignConstants.MK_DELAY_QUEUE, true);
    }

    @Bean
    public Binding mk_delay_binding() {
        return BindingBuilder.bind(mk_delay_queue()).to(mk_delay_exchange()).with(MkFeignConstants.MK_DELAY_ROUTING_KEY).noargs();
    }


    /*********************************** 营销活动-普通队列（优惠券过期/） *****************************************/
    @Bean
    public CustomExchange mk_normal_exchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MkFeignConstants.MK_NORMAL_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue mk_normal_queue() {
        return new Queue(MkFeignConstants.MK_NORMAL_QUEUE, true);
    }

    @Bean
    public Binding mk_normal_binding() {
        return BindingBuilder.bind(mk_normal_queue()).to(mk_normal_exchange()).with(MkFeignConstants.MK_NORMAL_ROUTING_KEY).noargs();
    }


    /*********************************** 营销活动-活动商品销量更新操作通知-加锁 *****************************************/

    @Bean
    Queue mk_goods_sales_queue() {
        return new Queue(MkFeignConstants.MK_GOODS_SALES_QUEUE, true, false, false);
    }

    @Bean
    DirectExchange mk_goods_sales_exchange() {
        return new DirectExchange(MkFeignConstants.MK_GOODS_SALES_EXCHANGE, true, false);
    }

    @Bean
    Binding mk_goods_sales_binding() {
        return BindingBuilder.bind(mk_goods_sales_queue()).to(mk_goods_sales_exchange()).with(MkFeignConstants.MK_GOODS_SALESROUTINGKEY);
    }

}
